package com.shopping.dao;

import com.shopping.pojo.Trade;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;

@Component
@Mapper
public interface TradeDao {

    String TABLE_NAME = "trade";
    String INSERT_FIELDS = "trade_name, trade_introduce, type_1, type_2, price, specification, inventory, " +
            "carriage, store_id, pic_1, pic_2, pic_3, pic_4, pic_5";
    String SELECT_FIELDS = "id, trade_name tradeName, trade_introduce tradeIntroduce, type_1 type1, type_2 type2, " +
            "price, specification, inventory, carriage, store_id storeId, pic_1 pic1, pic_2 pic2, pic_3 pic3, pic_4 pic4, pic_5 pic5";

    @Insert({"insert into ", TABLE_NAME, "(", INSERT_FIELDS, ") values(#{tradeName}, #{tradeIntroduce}, #{type1}, #{type2}, #{price}, #{specification}, #{inventory}, " +
            "#{carriage}, #{storeId}, #{pic1}, #{pic2}, #{pic3}, #{pic4}, #{pic5})"})
    int insert(Trade trade);

    @Delete({"delete from ", TABLE_NAME, " where id=#{tradeId}"})
    int deleteTrade(@Param("tradeId") int tradeId);

    @Select({"select ", SELECT_FIELDS, " from ", TABLE_NAME, " where id=#{id}"})
    Trade selectById(@Param("id") int id);

    @Select({"select ", SELECT_FIELDS, " from ", TABLE_NAME, " where store_id=#{storeId}"})
    List<Trade> selectByStoreId(@Param("storeId") int storeId);

    @Select({"select ", SELECT_FIELDS, " from ", TABLE_NAME, " where store_id=#{storeId} and trade_name=#{tradeName}"})
    List<Trade> selectByStoreAndTradeName(@Param("storeId") int storeId, @Param("tradeName") String tradeName);

    @Select({"select inventory from ", TABLE_NAME, " where id=#{id}"})
    int selectInventory(@Param("id") int id);

    @Update({"update ", TABLE_NAME, " set inventory=inventory-1 where id=#{id}"})
    int updateInventory(@Param("id") int id);

    @Update({"update ", TABLE_NAME, " set inventory=inventory+#{addNum} where id=#{id}"})
    int increaseInventory(@Param("id") int id, @Param("addNum") int addNum);

    @Select({"select store_id from ", TABLE_NAME, " where id=#{id}"})
    Integer selectStoreIdById(@Param("id") int id);

    @Select({"select type_1 from ", TABLE_NAME})
    Set<String> selectType();

    @Select({"select type_2 from ", TABLE_NAME, " where type_1=#{type}"})
    Set<String> selectTypeByType(@Param("type") String type);

    @Select({"select last_insert_id()"})
    Integer lastInsertId();

}
